package com.bosssoft.boss_exam_core.common.config.mail;


import com.bosssoft.boss_exam_core.common.helper.file.FileHelper;
import com.bosssoft.boss_exam_core.util.common.XmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.io.File;
import java.util.Properties;

@Configuration
public class MailSenderConfig {
    
    //文件处理工具注入
    @Autowired
    private FileHelper fileHelper;

    /***
     * @Author lys(cherish)
     * @Description 管理员邮箱配置对象交给spring管理
     * @Date  2019/2/26
     * @Param []
     * @return com.bosssoft.hr.jobmonitor.core.surport.AdminMail
     **/
    @Bean
    public AdminMail adminMail(){
        String xmlStr = fileHelper.readFile("resource"+ File.separator+"mail"+File.separator+"adminMail.xml");
        AdminMail adminMail = XmlUtil.toBean(xmlStr, AdminMail.class);
        System.out.println(adminMail);
        return adminMail;
    }
    /***
     * @Author lys(cherish)
     * @Description 邮件发送器注入spring
     * @Date  2019/2/26
     * @Param []
     * @return org.springframework.surport.javamail.JavaMailSender
     **/
    @Bean
    public JavaMailSender javaMailSender(){
        AdminMail adminMail = adminMail();
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(adminMail.getHost());
        javaMailSender.setUsername(adminMail.getUsername());
        javaMailSender.setPassword(adminMail.getPassword());
        javaMailSender.setDefaultEncoding(adminMail.getDefaultEncoding());
        Properties javaMailProperties = new Properties();
        javaMailProperties.setProperty("surport.smtp.auth",adminMail.getAuth());
        javaMailProperties.setProperty("surport.smtp.starttls.enable",adminMail.getEnable());
        javaMailProperties.setProperty("surport.smtp.starttls.required",adminMail.getRequired());
        // 阿里云25端口封禁，需要使用ssl加密,采用465端口
        javaMailProperties.setProperty("surport.smtp.ssl.enable","true");
        javaMailProperties.setProperty("surport.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        javaMailProperties.setProperty("surport.smtp.socketFactory.port","465");
        javaMailProperties.setProperty("surport.smtp.port","true");
        javaMailSender.setJavaMailProperties(javaMailProperties);
        return javaMailSender;
    }

}
